/*
 * agent.cpp
 *
 *  Created on: 2011-7-23
 *      Author: fufan
 */
#include "agent.h"

Agent::Agent(const QString &name, QComboBox *agentBox, QWidget *parent):QDialog(parent), qSettings("WhuCS", "QSnmp")
{
		setupUi(this);
		this->setModal(true);
		this->buttonBox->button(QDialogButtonBox::Ok)->setText("确定");
		this->buttonBox->button(QDialogButtonBox::Cancel)->setText("取消");
		this->listWidget->setFocus();

		this->agentBox = agentBox;

		readFromSetting(name);
		if(!this->snmp2->isChecked())
		{
			this->getBulk->setVisible(false);
		}
		this->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
		connect(this->listWidget, SIGNAL(customContextMenuRequested(const QPoint &)),this, SLOT(createItemMenu(const QPoint &)));
		connect(this->listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(displaySetting(QListWidgetItem *)));

		connect(this->name, SIGNAL(editingFinished()), this, SLOT(nameChange()));
		connect(this->snmp2, SIGNAL(toggled(bool )), this, SLOT(showGetBulk(bool)));
		connect(this, SIGNAL(accepted()), this, SLOT(saveToSettings()));
}

void Agent::readFromSetting(const QString &agentName)
{
		QStringList list = qSettings.childGroups();
		if(!list.isEmpty())
		{
			for(int i=0; i<list.size(); i++)
			{
				QString itemName = list.at(i);
				QListWidgetItem *item = new QListWidgetItem(itemName, listWidget);
				listWidget->addItem(item);
			}
			int index = list.indexOf(agentName);
			listWidget->setCurrentRow(index);
			QString group = listWidget->currentItem()->text();
			name->setText(group);
			setGroup(group);
		}
}

void Agent::clearDialog()
{
			name->clear();
			ipAddress->clear();
			port->clear();
			tries->clear();
			timeout->clear();
			read_community->clear();
			write_community->clear();
			snmp1->setChecked(false);
			snmp2->setChecked(false);
			Non_repeaters->clear();
			max_repetitions->clear();
}

void Agent::createItemMenu(const QPoint &pos)
{
		QMenu *menu = new QMenu(this->listWidget);
		QPoint gPos = this->listWidget->mapToGlobal(pos);

		QAction *addAction = new QAction("添加代理",  menu);
		QAction *deleteAction = new QAction("删除代理",  menu);
		QAction *saveAction = new QAction("保存代理", menu);

		menu->addAction(addAction);
		menu->addAction(deleteAction);
		menu->addAction(saveAction);

		connect(addAction, SIGNAL(triggered(bool )), this, SLOT(addAgent()));
		connect(deleteAction, SIGNAL(triggered(bool)), this, SLOT(deleteAgent()));
		connect(saveAction, SIGNAL(triggered(bool)), this, SLOT(saveAgent()));

		menu->popup(gPos);
}

void Agent::showGetBulk(bool checked)
{

	 this->getBulk->setVisible(checked);
	 this->adjustSize();
}

void Agent::saveToSettings()
{
			QString name = this->name->text();
			if(name.trimmed() == "")
			{
				return;
			}
			saveGroup(name);
}

void Agent::addAgent()
{
			clearDialog();
			name->setText("新代理");
			QListWidgetItem *item = new QListWidgetItem("新代理", listWidget);
			listWidget->addItem(item);
			listWidget->setCurrentItem(item);

}

void Agent::deleteAgent()
{
			QListWidgetItem *item = listWidget->currentItem();
			if(item == NULL)
			{
				return;
			}
			QString group = item->text();
			qSettings.beginGroup(group);
			qSettings.remove("");
			qSettings.endGroup();
			delete item;

			clearDialog();
			int index = listWidget->count()-1;
			if(index < 0)
			{
				clearDialog();
			}
			{
				listWidget->setCurrentRow(index);
				displaySetting(listWidget->currentItem());
			}

			//删除ComoboBox中的项
			agentBox->removeItem(agentBox->findText(group));
}

void Agent::saveAgent()
{
		QListWidgetItem *item = listWidget->currentItem();
		if(item == NULL)
		{
			return;
		}
		QString group = item->text();
		saveGroup(group);

		//增加ComoboBox中的项
		if(agentBox->findText(group) == -1)
		{
			agentBox->addItem(group);
		}

}

void Agent::displaySetting(QListWidgetItem *item)
{
			if(item == NULL)
							return;
			clearDialog();
			QString group = item->text();
			name->setText(group);
			setGroup(group);
}

void Agent::nameChange()
{
			if(name->text().trimmed() == "")
			{
				return;
			}
			QListWidgetItem *item = listWidget->currentItem();
			if(item == NULL)
			{
				return;
			}
			QString groupName = item->text();
			item->setText(name->text());

			qSettings.remove(groupName);
			saveGroup(name->text());

}

void Agent::saveGroup(const QString &group)
{
			qSettings.beginGroup(group);
			qSettings.setValue("ipAddress", ipAddress->text());
			qSettings.setValue("port", port->text());
			qSettings.setValue("tries", tries->text());
			qSettings.setValue("timeout", timeout->text());
			qSettings.setValue("read_community", read_community->text());
			qSettings.setValue("write_community", write_community->text());
			qSettings.setValue("snmp1", snmp1->isChecked());
			qSettings.setValue("snmp2", snmp2->isChecked());
			qSettings.setValue("non_repeaters", Non_repeaters->text());
			qSettings.setValue("max_repetitions", max_repetitions->text());
			qSettings.endGroup();
}

void Agent::setGroup(const QString &group)
{
			qSettings.beginGroup(group);
			ipAddress->setText(qSettings.value("ipAddress").toString());
			port->setText(qSettings.value("port").toString());
			tries->setValue(qSettings.value("tries").toInt());
			timeout->setValue(qSettings.value("timeout").toInt());
			read_community->setText(qSettings.value("read_community").toString());
			write_community->setText(qSettings.value("write_community").toString());
			snmp1->setChecked(qSettings.value("snmp1").toBool());
			snmp2->setChecked(qSettings.value("snmp2").toBool());
			Non_repeaters->setValue(qSettings.value("non_repeaters").toInt());
			max_repetitions->setValue(qSettings.value("max_repetitions").toInt());
			qSettings.endGroup();
}











